﻿<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ServiceDefinitionSchema"
    targetNamespace="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"
    elementFormDefault="qualified"
    xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version ="1.0"
>
  <xs:complexType name="LocalStore">
    <xs:annotation>
      <xs:documentation>
        Configuration of a local storage resource
      </xs:documentation>
    </xs:annotation>
    <xs:attribute name="name" use="required" type="NamedElementNameString">
      <xs:annotation>
        <xs:documentation>
          The name of the resource. This is used by the role's code to map to a directory in the local file system.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="sizeInMB" use="optional" type="xs:int">
      <xs:annotation>
        <xs:documentation>
          The desired storage space (in MB)
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="cleanOnRoleRecycle" use="optional" type="xs:boolean" default="true">
      <xs:annotation>
        <xs:documentation>
          Deletes the contents of the local storage on role recycle.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>

  <xs:complexType name="ConfigurationSetting">
    <xs:annotation>
      <xs:documentation>
        A configuration setting is the mapping of a name to a string value. The name of the setting is
        declared here and the mapping to a value is set in the service configuration file.
      </xs:documentation>
    </xs:annotation>
    <xs:attribute name="name" type="NamedElementNameString" use="required">
      <xs:annotation>
        <xs:documentation>
          The name of the configuration setting.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>

  <xs:complexType name="Certificate">
    <xs:annotation>
      <xs:documentation>
        A certificate setting is the mapping of a named  certificate to its destination. 
        The mapping from the name of the certificate to the actual certificate is defined in the service configuration file.
      </xs:documentation>
    </xs:annotation>
    <xs:attribute name="name" type="NamedElementNameString" use="required">
      <xs:annotation>
        <xs:documentation>
          The name of the certificate.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="storeLocation" type="StoreLocation" use="required">
      <xs:annotation>
        <xs:documentation>
          The location of the store where the certificate needs to be deployed.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="storeName" type="StoreName" use="required">
      <xs:annotation>
        <xs:documentation>
          The name of the store where the certificate needs to be deployed. 
          Either built in stores like My (for Personal), Root(for Trusted Root CA), CA (for Intermediate CA), Trust (for Enterprise Trust) 
          or a custom certificate store
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>

  <xs:simpleType name="StoreLocation">
    <xs:restriction base="xs:string">
      <xs:enumeration value="CurrentUser" >
        <xs:annotation>
          <xs:documentation>
            To install to the users certificate stores
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="LocalMachine" >
        <xs:annotation>
          <xs:documentation>
            To install to the machines certificate stores
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>
    </xs:restriction>
  </xs:simpleType>
 
  <xs:simpleType name="RoleSize">
      <xs:restriction base="xs:string">
      <xs:enumeration value="Small" />
      <xs:enumeration value="Medium" />
      <xs:enumeration value="Large" />
      <xs:enumeration value="ExtraLarge" />
      </xs:restriction>
  </xs:simpleType>
  
  <xs:simpleType name="StoreName">
    <xs:union memberTypes="BuiltinStoreName NonEmptyString"/>
  </xs:simpleType>

  <xs:simpleType name="BuiltinStoreName">
    <xs:restriction base="xs:string">     
      <xs:enumeration value="My" >
        <xs:annotation>
          <xs:documentation>
            Personal certificate store
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="Root" >
        <xs:annotation>
          <xs:documentation>
            Trusted Root Certificate Authorities certificate store
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="CA" >
        <xs:annotation>
          <xs:documentation>
            Intermediate Certificate Authorities certificate store
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="Trust" >
        <xs:annotation>
          <xs:documentation>
            Enterprise Trust List certificate store
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="Disallowed" >
        <xs:annotation>
          <xs:documentation>
            Untrusted Certificates
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="TrustedPeople" >
        <xs:annotation>
          <xs:documentation>
            Trusted People certificate store
          </xs:documentation>
        </xs:annotation>        
      </xs:enumeration>
      <xs:enumeration value="TrustedPublisher" >
        <xs:annotation>
          <xs:documentation>
            Trusted Publishers certificate store
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="AuthRoot" >
        <xs:annotation>
          <xs:documentation>
            Third-party certificate authorities certificate store
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>            
      <xs:enumeration value="AddressBook" >
        <xs:annotation>
          <xs:documentation>
            Other people certificate store
          </xs:documentation>
        </xs:annotation>
      </xs:enumeration>
    </xs:restriction>
  </xs:simpleType>
  
  <xs:complexType name="Certificates">
    <xs:annotation>
      <xs:documentation>
        Declaration of all certificates that need to be deployed.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="Certificate" type="Certificate" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="WebRole">
    <xs:annotation>
      <xs:documentation>
        The configuration of the web role which accepts external requests.
      </xs:documentation>
    </xs:annotation>
    <xs:all>
      <xs:element name="LocalStorage" type="LocalStore" minOccurs="0" maxOccurs="1" />
      <xs:element name="LocalResources" type="LocalResources" minOccurs="0" maxOccurs="1" />
      <xs:element name="ConfigurationSettings" minOccurs="0" maxOccurs="1">
        <xs:annotation>
          <xs:documentation>
            Declaration of names
            Configuration settings for a role are name-value pairs that can be configured in the instance
            settings of the service and are accessed by name from the role's code.
          </xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Setting" type="ConfigurationSetting" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="InputEndpoints" minOccurs="0" maxOccurs="1">
        <xs:complexType>
          <xs:annotation>
            <xs:documentation>
              A set of endpoints that can be exposed by the service.
            </xs:documentation>
          </xs:annotation>
          <xs:sequence minOccurs="1" maxOccurs="2">
            <xs:element name="InputEndpoint">
              <xs:annotation>
                <xs:documentation>
                  An endpoint of a service specifies the transport protocol and port on which the service receives requests.
                </xs:documentation>
              </xs:annotation>
              <xs:complexType>
                <xs:complexContent>
                  <xs:restriction base="InputEndpoint">
                    <xs:attribute name="protocol" use="required" >
                      <xs:simpleType>
                        <xs:restriction base="Protocol">
                          <xs:enumeration value="http" />
                          <xs:enumeration value="https" />
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:attribute>
                  </xs:restriction>
                </xs:complexContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="InternalEndpoint" minOccurs="0" maxOccurs="1">
        <xs:complexType>
          <xs:complexContent>
            <xs:restriction base="InternalEndpoint">
              <xs:attribute name="protocol" use="required" >
                <xs:simpleType>
                  <xs:restriction base="InternalProtocol">
                    <xs:enumeration value="http" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:attribute>
            </xs:restriction>
          </xs:complexContent>
        </xs:complexType>
      </xs:element>
      <xs:element name="Certificates" type="Certificates" minOccurs="0" maxOccurs="1" />
     </xs:all>
    <xs:attribute name="name" type="NamedElementNameString" use="required">
      <xs:annotation>
        <xs:documentation>
          The name of the role. Each role should have a unique name.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="enableNativeCodeExecution" type="xs:boolean" default="true">
      <xs:annotation>
        <xs:documentation>
          Allow the execution of native x64 code in this role. Also enables full trust managed code.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="vmsize" type="RoleSize" default="Small">
      <xs:annotation>
        <xs:documentation>
          The size of the VM needed to host this role.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
   </xs:complexType>

  <xs:complexType name="WorkerRole">
    <xs:annotation>
      <xs:documentation>
        Configuration for optional process role of service.
      </xs:documentation>
    </xs:annotation>
    <xs:all>
      <xs:element name="LocalStorage" type="LocalStore" minOccurs="0" maxOccurs="1" />
      <xs:element name="LocalResources" type="LocalResources" minOccurs="0" maxOccurs="1" />
      <xs:element name="ConfigurationSettings" minOccurs="0" maxOccurs="1">
        <xs:annotation>
          <xs:documentation>
            Configuration settings are deployment-specific mappings of names to string values that can be
            accessed from the role's code. The names of the settings are declared here and the mappings to
            values is done in the service configuration file.
          </xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Setting" type="ConfigurationSetting" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="Endpoints" minOccurs="0" maxOccurs="1">
        <xs:complexType>
          <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="InputEndpoint" type="InputEndpoint" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element name="InternalEndpoint" type="InternalEndpoint" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="Certificates" type="Certificates" minOccurs="0" maxOccurs="1" />
    </xs:all>
    <xs:attribute name="name" type="NamedElementNameString" use="required">
      <xs:annotation>
        <xs:documentation>
          The name of the role. Each role should have a unique name.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="enableNativeCodeExecution" type="xs:boolean" default="true">
      <xs:annotation>
        <xs:documentation>
          Allow the execution of native x64 code in this role. Also enables full trust managed code.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="vmsize" type="RoleSize" default="Small">
      <xs:annotation>
        <xs:documentation>
          The size of the VM needed to host this role.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>

  <xs:simpleType name="Protocol">
    <xs:restriction base="xs:string">
      <xs:enumeration value="http"/>
      <xs:enumeration value="https" />
      <xs:enumeration value="tcp" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="InternalProtocol">
    <xs:restriction base="Protocol">
      <xs:enumeration value="http"/>
      <xs:enumeration value="tcp"/>
    </xs:restriction>
  </xs:simpleType>
  
  <xs:complexType name="InputEndpoint">
    <xs:annotation>
      <xs:documentation>
        An endpoint of a service specifies the transport protocol and port on which the service receives requests.
      </xs:documentation>
    </xs:annotation>
    <xs:attribute name="name" type="NamedElementNameString" use="required">
      <xs:annotation>
        <xs:documentation>
          The name of the endpoint.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="protocol" type="Protocol" use="required"/>
    <xs:attribute name="port"  type="xs:int" use="required" />
    <xs:attribute name="certificate" type="NamedElementNameString">
      <xs:annotation>
        <xs:documentation>
          The name of the certificate for use with https endpoints. 
          The store location and store names for https configuration is automatic and need not be defined.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>

  <xs:complexType name="InputEndpoints" >
    <xs:annotation>
      <xs:documentation>
        A set of endpoints that can be exposed by the service.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="InputEndpoint" type="InputEndpoint"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="InternalEndpoint">
    <xs:annotation>
      <xs:documentation>
        An endpoint of a role that can be used for inter role communication. It specifies the transport protocol.
      </xs:documentation>
    </xs:annotation>
    <xs:attribute name="name" type="NamedElementNameString" use="required">
      <xs:annotation>
        <xs:documentation>
          The name of the endpoint
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="protocol" use="required" type="InternalProtocol">
      <xs:annotation>
        <xs:documentation>
          The protocol of the endpoint
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>

  <xs:complexType name="InternalEndpoints" >
    <xs:annotation>
      <xs:documentation>
        A set of internal endpoints that can be used for inter-role communication by the service.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="InternalEndpoint" type="InternalEndpoint"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ServiceDefinition">
    <xs:annotation>
      <xs:documentation>
        The description of a Utility Computing Service.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence minOccurs="1" maxOccurs="unbounded">
      <xs:element name="WebRole" type="WebRole" minOccurs="0" maxOccurs="unbounded" />
      <xs:element name="WorkerRole" type="WorkerRole" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="name" type="NamedElementNameString" use="required">
      <xs:annotation>
        <xs:documentation>
          The name of the service.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
    <xs:attribute name="upgradeDomainCount" type="xs:positiveInteger" use="optional">
      <xs:annotation>
        <xs:documentation>
          The number of upgrade domains for each role in this service.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>

  <xs:complexType name="LocalResources">
    <xs:annotation>
      <xs:documentation>
        A set of local storage resources that can be defined for the service.
      </xs:documentation>
    </xs:annotation>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="LocalStorage" type="LocalStore" />
    </xs:sequence>
  </xs:complexType>

  <xs:element name="ServiceDefinition" type="ServiceDefinition"/>

  <xs:simpleType name="NamedElementNameString">
    <xs:restriction base="xs:string">
      <xs:pattern value="^[a-zA-Z_][^\\\/\:\*\?\&quot;\&lt;\&gt;\|]*(?&lt;![\.\s])$" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="NonEmptyString">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:maxLength value="256"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
